<?php 
class login_model extends CI_Model{
	function login_model()
	{
		 'parent'||model();
	}


	 function login($name,$password)
         {
			 $query = "SELECT * FROM users where name='$name'and password='$password'and status=1";
	         $result=$this->db->query($query); 
            
         
	    if( $result->num_rows() > 0 ){
		   
		  return $result;
		  }
		  else{
		   return false;
		  }
	 
}
	 function insertmultiplequestion($course_id, $quest,$answer,$userId,$opt1,$opt2,$opt3,$opt4,$opt5,$chapterno,$muliplechoice)
         {
$query = "INSERT INTO multiplechoicequestions (courseId, question, answer, userId,status,opt1,opt2,opt3,opt4,opt5,chapterno,multiplechoice)
           VALUES ('$course_id', '$quest', '$answer','$userId','0','$opt1','$opt2','$opt3','$opt4','$opt5','$chapterno','$muliplechoice')";
	         $result=$this->db->query($query); 
              return $result;
	 
}


function insertquestion($course_id, $quest,$answ,$userId)
         {

			 $query = "INSERT INTO questions (courseId, question, answer, userId,status)
                                   VALUES ('$course_id', '$quest', '$answ','$userId','0')";
	         $result=$this->db->query($query); 
              return $result;
	 
}



function getcourse()
         {
			 $query = "SELECT * FROM courses where status='1'";
	         $result=$this->db->query($query); 
  
         
	   if( $result->num_rows() > 0 ){
		   
		  return $result;
		 }
		 else{
		  return false;
		 }



}
function getallquestions()
         {
			 $query = "SELECT * FROM questions where status=0";
	         $result=$this->db->query($query); 
  
         
	   if( $result->num_rows() > 0 ){
		   
		  return $result;
		 }
		 else{
		  return false;
		 }

}

function loadcoursequestion($courseid,$chapterno,$multiplechoice)
	{
$query = "SELECT cou.courseName,cch.name,mpq.id,mpq.question,mpq.opt1,mpq.opt2,mpq.opt3,mpq.opt4,mpq.opt5
FROM multiplechoicequestions mpq ,courses cou,course_chapters cch where mpq.chapterno=cou.id and mpq.courseId='$courseid' and cou.id='$courseid' and mpq.multiplechoice='$multiplechoice'";
if($chapterno!="100")
		{
$query=$query."and mpq.chapterno='$chapterno' and cch.chapterno=mpq.chapterno";
		}
	         $result=$this->db->query($query); 
  
         
	   if( $result->num_rows() > 0 ){
		   
		  return $result;
		 }
		 else{
		  return false;
		 }

}

function insertcourse($coursename,$description)
         {

			 $query = "INSERT INTO courses (courseName,description,status)
                                   VALUES ('$coursename', '$description','1')";
	         $result=$this->db->query($query); 
              return $result;
	 
}


function getcoursechapters($courseid)
	{
$query = "SELECT * FROM course_chapters where courseid='$courseid' and status=1";
	         $result=$this->db->query($query); 
  
         
	   if( $result->num_rows() > 0 ){
		   
		  return $result;
		 }
		 else{
		  return false;
		 }

	}


function getstudent($courseid)
	{
	$query = "SELECT * FROM users where courseid='$courseid' and status=1";
	         $result=$this->db->query($query); 
  
         
	   if( $result->num_rows() > 0 ){
		   
		  return $result;
		 }
		 else{
		  return false;
		 }
	}


function loadassignments($courseid,$chapterno,$multiplechoice)
	{
	$query = "SELECT * FROM assignments where courseid='$courseid' and chapterno='$chapterno' and multiplechoice='$multiplechoice' and status=1";
	         $result=$this->db->query($query); 
  
         
	   if( $result->num_rows() > 0 ){
		   
		  return $result;
		 }
		 else{
		  return false;
		 }
	}


function insertstudent($name, $contactno,$email,$address,$courseid,$password)
         {

			 $query = "INSERT INTO users (name, contactno, email, address,courseid,password,status,rollId)
                                   VALUES ('$name', '$contactno', '$email','$address','$courseid','$password','1','3')";
	         $result=$this->db->query($query); 
              return $result;
	 
}


function assignassignmenttostudents($studentIds,$assignmentsIds)
	{

 

	$students = explode(",", $studentIds);
	$assignments = explode(",", $assignmentsIds);
	$result="";
     for($i=0; $i<sizeof($students); $i++)
		{
		 for($j=0; $j<sizeof($assignments); $j++)
			{

			 $query = "INSERT INTO assignedassignments (assignmentid, studentid, status,assigneddate,validdate)
                       VALUES ('$assignments[$j]','$students[$i]','1',now(),DATE_ADD(now(),INTERVAL 7 DAY))";
	         $result=$this->db->query($query); 
			}
		}
  return $result;
	}


function selectcoursestudents($courseid)
	{
	$query = "SELECT * FROM users where courseid='$courseid' and status=1";
	         $result=$this->db->query($query); 
  
         
	   if( $result->num_rows() > 0 ){
		   
		  return $result;
		 }
		 else{
		  return false;
		 }
	}
function updatepassword($userId,$oldpassword,$newpassword,$uname)

	{
		 $verifyoldpassword=$this->login($uname,$oldpassword);
		 if($verifyoldpassword!=false){
		$query = "Update users  set password='$newpassword' where password='$oldpassword' and id='$userId' and status=1";
	         $result=$this->db->query($query); 
             return "Password Changed";
			 }

			 else{
			 return "Old Password Is Incorrect";
			 }
  
	}


	function forgotpassword($emailId,$randomstring)
	     {

	    $query = "Update users  set randomstring='$randomstring' where email='$emailId'and status=1";
	         $result=$this->db->query($query); 
             return $result;
			 }
  function updateforgotpassword($email,$randomlink,$newpass)
	     {

	    $query = "Update users  set password='$newpass',randomstring='0' where email='$email'and randomstring='$randomlink' and status=1";
	         $result=$this->db->query($query); 
             return $result;
			 }



function insertassignment($courseid,$chapterno,$assignname,$assgdesc,$questionarray,$uid,$optionmultiple)
	{
$query = "INSERT INTO assignments (name, description, courseid, chapterno,questionno,status,addby,multiplechoice)
                                   VALUES ('$assignname', '$assgdesc', '$courseid','$chapterno','$questionarray','1','$uid','$optionmultiple')";
	         $result=$this->db->query($query); 
              return $result;
	}


function getassignments()
	{
$query = "SELECT * FROM assignments where status=1";
	         $result=$this->db->query($query); 
  
         
	   if( $result->num_rows() > 0 ){
		   
		  return $result;
		 }
		 else{
		  return false;
		 }

	}
function savepopupemail($email)
	{
			$query = "INSERT INTO popupinfi (email) VALUES ('$email')";
	        $result=$this->db->query($query); 
            return $result;
	}
function getuserassignments($studentId)
	{
$query = "SELECT asad.id, cav.courseName as Course, cochpt.name as Chapter,asa.name as AssignmentName, 
asa.description as Description , asa.assignmentid
FROM assignedassignments asad, assignments asa,
 courses_available cav, course_chapters cochpt where asad.studentid='$studentId' and asad.status=1 and 
 asad.assignmentid=asa.assignmentid and
 cav.id=asa.courseid and cochpt.chapterno=asa.chapterno and asad.validdate >= now()";
	         $result=$this->db->query($query); 
  
         
	   if( $result->num_rows() > 0 ){
		   
		  return $result;
		 }
		 else{
		  return false;
		 }

	}



function getassignmentquestion($assgnmentId,$assignmentrowid)
	{
	 

$query = " select * from (select * from multiplechoicequestions mcq where 
 FIND_IN_SET(mcq.id,(select asm.questionno from assignments asm where asm.assignmentid='$assgnmentId'))) m 
 where !FIND_IN_SET( m.id ,(select questionattempted from assignedassignments where id='$assignmentrowid'))";
 


	         $result=$this->db->query($query); 
      if( $result->num_rows() > 0 ){
		  return $result;
		 }
		 else{
		  return false;
		 }


	}


function submitattemptedassignment($assignmetid,$questionno,$answer)
	{
 $questans=$questionno."-".$answer;
 $query = "Update assignedassignments  set answer=CONCAT(answer,'$questans') , questionattempted=CONCAT(questionattempted,'$questionno'), status='0' where id='$assignmetid'";
$result=$this->db->query($query);
$score=$this->calculatemarks($assignmetid);
$query = "Update assignedassignments  set  marks='$score' where id='$assignmetid'";
$result=$this->db->query($query);
echo "Assignment Submitted Successfully \n";
echo " \r  Your Total Score is ".$score;
     }

function calculatemarks($assignmentrowid)
	{

$query = "SELECT answer FROM assignedassignments where id='$assignmentrowid'";
	         $ansresult=$this->db->query($query); 
  $marks=0;
         $answerlist="";
	   if( $ansresult->num_rows() > 0 ){
foreach ($ansresult->result() as $row)
       { 
$answerlist=$row->answer;
$answersarray = explode(",", $answerlist);

for($i=0; $i<sizeof($answersarray); $i++)
		{
	$ansstr="";
	$flage=true;
	$answerobj = explode("-", $answersarray[$i]);
     if(preg_match('**',$answerobj[1])) {

          $answersstr=explode("**", $answerobj[1]);
               }
          else{
         $ansstr=$answerobj[1];
		 $flage=false;;
              }

  if($flage){
 for($j=0; $j<sizeof($answersstr); $j++)
		{
	 if($j==0){
      $ansstr=$answersstr[$j];
	 }

	 else
			{
		 $ansstr=$ansstr.",".$answersstr[$j];
			}
		}
  }
$query = "select * from multiplechoicequestions where id='$answerobj[0]' and answer='$ansstr'";
 
	         $result=$this->db->query($query); 
      if( $result->num_rows() > 0 ){
		  $marks++;
		 }

  
		}
	   }
	   }
return $marks;

	}



function assignmentcurrentstatus($assignmetid,$questionno,$answer)
	{
	$questans=$questionno."-".$answer;


/*$query = "select * from assignedassignments where id='$assignmetid'";
$result=$this->db->query($query); 
      if( $result->num_rows() > 0 ){
		  foreach ($result->result() as $row)
            {
			  echo $row->answer;
 $dbquestans=$row->answer.",".$questans;
 echo $dbquestans;
			}*/

$query = "Update assignedassignments set answer=CONCAT(answer,'$questans',','), questionattempted=CONCAT(questionattempted,'$questionno',',') where id='$assignmetid'";
 
	         $result=$this->db->query($query);
		   
		 }



function getassignmentresult($userid)
	{
$query = "select asg.name,asg.description, asad.assigneddate, asad.marks from assignedassignments asad, assignments asg where asad.studentid='$userid' and asad.status='0' and asad.id=asg.assignmentid";
 
 $result=$this->db->query($query); 
      if( $result->num_rows() > 0 ){
		  return $result;
		 }
		 else{
		  return false;
		 }
}

	
function getvalidedate()
	{


	}




}

 
 ?>